﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ButtonBehavior : MonoBehaviour
{
    private bool m_init = false;
    private Button m_btn;
    private Toggle m_tgl;
    private System.Action<object[], GameObject> onClickEvent;
    private System.Action<object[], bool, GameObject> onValueChangeEvent;
    private object[] data;
    public void Initialized(object[] data, System.Action<object[], bool, GameObject> onValueChangeEvent)
    {
        Release();
        this.data = data;
        this.onValueChangeEvent = onValueChangeEvent;
        m_tgl = gameObject.GetComponent<Toggle>();
        m_btn = gameObject.GetComponent<Button>();
        if (m_btn != null)
        {
            m_btn.onClick.RemoveListener(OnClickAction);
            m_btn.onClick.AddListener(OnClickAction);
        }
        else if (m_tgl != null)
        {
            m_tgl.onValueChanged.RemoveListener(OnValueChange);
            m_tgl.onValueChanged.AddListener(OnValueChange);
        }
        else
        {
            EventTriggerListener.Get(gameObject).onClick -= OnClickAction;
            EventTriggerListener.Get(gameObject).onClick += OnClickAction;
        }
        m_init = true;
    }
    public void Initialized(object[] data, System.Action<object[], GameObject> onClickEvent)
    {
        Release();
        this.data = data;
        this.onClickEvent = onClickEvent;
        m_tgl = gameObject.GetComponent<Toggle>();
        m_btn = gameObject.GetComponent<Button>();
        if (m_btn != null)
        {
            m_btn.onClick.RemoveListener(OnClickAction);
            m_btn.onClick.AddListener(OnClickAction);
        }
        else if (m_tgl != null)
        {
            m_tgl.onValueChanged.RemoveListener(OnValueChange);
            m_tgl.onValueChanged.AddListener(OnValueChange);
        }
        else
        {
            EventTriggerListener.Get(gameObject).onClick += OnClickAction;
        }
        m_init = true;
    }
    private void OnValueChange(bool isOn)
    {
        if (this.onClickEvent != null && isOn)
            this.onClickEvent(this.data, gameObject);
        if (this.onValueChangeEvent != null)
            this.onValueChangeEvent(this.data, isOn, gameObject);
    }
    private void OnClickAction()
    {       
        OnClickAction(gameObject);
    }
    private void OnClickAction(GameObject go)
    {
        if (this.onClickEvent != null)
            this.onClickEvent(this.data, go);
        if (this.onValueChangeEvent != null)
            this.onValueChangeEvent(this.data, true, go);
    }

    public void Release()
    {
        if (m_init)
        {
            if (m_btn != null)
            {
                m_btn.onClick.RemoveListener(OnClickAction);
            }
            else if (m_tgl != null)
            {
                m_tgl.onValueChanged.RemoveListener(OnValueChange);
            }
            else
            {
                EventTriggerListener.Get(gameObject).onClick -= OnClickAction;
            }
            data = null;
            onClickEvent = null;
            onValueChangeEvent = null;
            m_init = false;
            m_btn = null;
            m_tgl = null;
        }
    }

}
